package tds.model2.version2;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import org.apache.commons.lang3.math.NumberUtils;

/**
 * 
 * @Company 杭州先锋电子技术股份有限公司
 * @ClassName CashierConsole
 * @Description TODO(收银台)
 * @author tds
 * @Date 2019年1月7日 上午9:19:44
 * @version 1.0.0
 */
public class CashierConsole {
	public static void main(String[] args) {
		JFrame frame = new JFrame("收银台");
		frame.setSize(400, 400);
		frame.setLocation(100, 100);
		//边界布局
		frame.setLayout(new BorderLayout());
		
		JPanel northPannel = new JPanel();
		JPanel southPannel = new JPanel();
		JScrollPane centerPannel = new JScrollPane();
		
		//网格布局
		northPannel.setLayout(new GridLayout(3, 3));
		
		JLabel label1 = new JLabel("单价");
		JLabel label2 = new JLabel("数量");
		JLabel label3 = new JLabel("计算方式");
		JTextField textField1 = new JTextField();
		JTextField textField2 = new JTextField();
		JButton btn1 = new JButton("确定");
		JButton btn2 = new JButton("重置");
		JComboBox<String> jComboBox = new JComboBox<>(new String[]{"正常收费", "6折", "8折", "9折", "满300返100"});
		northPannel.add(label1);
		northPannel.add(textField1);
		northPannel.add(btn1);
		northPannel.add(label2);
		northPannel.add(textField2);
		northPannel.add(btn2);
		northPannel.add(label3);
		northPannel.add(jComboBox);
		
		JTextArea textArea = new JTextArea(10, 10);
		textArea.setEditable(false);
		textArea.setBounds(10, 10, 300, 200);
		centerPannel.setViewportView(textArea);
		
		JLabel label4 = new JLabel("总计：");
		southPannel.add(label4);
		
		frame.add(northPannel, BorderLayout.NORTH);
		frame.add(centerPannel);
		frame.add(southPannel, BorderLayout.SOUTH);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		
		btn1.addActionListener(new ActionListener() {
			BigDecimal total = new BigDecimal("0");
			@Override
			public void actionPerformed(ActionEvent e) {
				BigDecimal price;
				int num = 0;
				BigDecimal totalPrice;
				if(NumberUtils.isNumber(textField1.getText().trim())){
					price = new BigDecimal(textField1.getText().trim());
				}else{
					JOptionPane.showMessageDialog(frame, "单价格式不对");
					return;
				}
				
				if(NumberUtils.isNumber(textField2.getText().trim())){
					num = Integer.valueOf(textField2.getText().trim());
				}else{
					JOptionPane.showMessageDialog(frame, "数量格式不对");
					return;
				}
				totalPrice = price.multiply(BigDecimal.valueOf(num));
				
				
				int selectedIndex = jComboBox.getSelectedIndex();
				
				CashContext context = new CashContext(CashType.valueOf(selectedIndex));
				BigDecimal result = context.getResult(totalPrice);
				total = total.add(result);
				textArea.append("单价:" + price + ", 数量:" + num + ", 折扣:" + jComboBox.getSelectedItem()  + ", 合计:" + result  + "\n");
				label4.setText("总计：" + total);
			}
		});
	}
}
